package com.lzit.smart_canteen.controller;

import com.lzit.smart_canteen.domain.CartItem;
import com.lzit.smart_canteen.domain.Order;
import com.lzit.smart_canteen.domain.OrderItem;
import com.lzit.smart_canteen.domain.User;
import com.lzit.smart_canteen.service.ItemService;
import com.lzit.smart_canteen.service.OrderService;
import com.lzit.smart_canteen.utils.TimeUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Controller
public class OrderController {
    @Autowired
    ItemService itemService;
    @Autowired
    OrderService orderService;
    @Autowired
    TimeUtil timeUtil;
    //生成订单(购物车结算)
    @GetMapping("/toOrder")
    private String toOrder(HttpServletRequest request, Model model) throws ParseException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        List<CartItem> cartItems = itemService.queryCart(uid);

        if (cartItems.isEmpty()){
            return "/mobile/mobile_template";
        }
        //获取当前时间
        Timestamp date = new Timestamp(TimeUtil.formatYmd_hms().getTime());
        String orderDate = TimeUtil.formatYmdhms();

        //UUID生成订单号
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString();
        String orderId = orderDate+"-"+uuidStr;

        BigDecimal total = new BigDecimal(0);
        for (CartItem item:cartItems){
            OrderItem orderItem = new OrderItem();
            orderItem.setFname(item.getFname());
            orderItem.setWindow(item.getWindow());
            orderItem.setStatus("unpay");
            orderItem.setPrice(item.getPrice());
            orderItem.setCount(item.getCount());
            orderItem.setFid(item.getFid());
            orderItem.setUid(item.getUid());
            orderItem.setTime(date);
            orderItem.setOid(orderId);
            itemService.addItem(orderItem);
            total = total.add(item.getPrice().multiply(BigDecimal.valueOf(item.getCount())));
        }
        //创建订单
        Order order = new Order();
        order.setOid(orderId);
        order.setStatus("unpay");
        order.setPrice(total);
        order.setTime(date);
        order.setUid(uid);
        orderService.addOrder(order);
        //清空购物车
        itemService.deleteCartItemByUid(uid);

        model.addAttribute("order",order);
        model.addAttribute("pageName", "待付款");
        return "/mobile/addOrder";
    }
    //取消订单
    @GetMapping("/cancelOrder")
    private String cancelOrder(HttpServletRequest request) {
        String oid = request.getParameter("oid");
        //清空该订单所有Item
        itemService.deleteOrderItemsByOid(oid);
        orderService.deleteOrderByOid(oid);
        return "/mobile/mobile_template";
    }
    //支付订单
    @GetMapping("/payOrder")
    private String payOrder(HttpServletRequest request,Model model) {
        String oid = request.getParameter("oid");
        Order order = orderService.findOrderByOid(oid);
        if (Objects.equals(order.getStatus(), "process")){
            return "/mobile/mobile_template";
        }
        order.setStatus("process");
        List<OrderItem> orderItems = itemService.findOrderItemsByOid(oid);
        for (OrderItem item:orderItems){
            itemService.updateOrderItem("process",item.getOid());
        }
        model.addAttribute("pageName", "付款成功");
        return "/mobile/paySuccess";
    }
    //出餐
    @GetMapping("/finishItem")
    public void finishItem(HttpServletRequest request,HttpServletResponse response) {
        String oid = request.getParameter("oid");
        Integer fid = Integer.valueOf(request.getParameter("fid"));
        itemService.updateItemStatusByOidAndFid("finish",oid,fid);
        List<OrderItem> orderItems = itemService.findOrderItemsByOidAndStatus(oid,"process");
        if (orderItems.isEmpty()){
            orderService.updateStatusByOid(oid,"finish");
        }
    }
    //取餐
    @GetMapping("/getItem")
    public void getItem(HttpServletRequest request,HttpServletResponse response){
        String oid = request.getParameter("oid");
        Integer fid = Integer.valueOf(request.getParameter("fid"));
        itemService.updateItemStatusByOidAndFid("OK",oid,fid);
        List<OrderItem> orderItems = itemService.findOrderItemsByOidAndStatus(oid,"process");
        if (orderItems.isEmpty()){
            orderService.updateStatusByOid(oid,"OK");
        }
    }

}
